﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using litsdk;

namespace litssh
{
    internal partial class ShellStreamUI : litsdk.ILitUI
    {
        public ShellStreamUI()
        {
            InitializeComponent();
            this.ivHost.ShowVarName(true, false, true, this.tbHost);
            this.ivPort.ShowVarName(true, false, true, this.tbPort);
            this.ivUserName.ShowVarName(true, false, true, this.tbUserName);
            this.ivPassword.ShowVarName(true, false, true, this.tbPassword);

            this.SaveActivity = () =>
            {
                ssa.Host = this.tbHost.Text;
                ssa.Port = this.tbPort.Text;
                ssa.UserName = this.tbUserName.Text;
                ssa.Password = this.tbPassword.Text;
                ssa.Name = this.tbActivityName.Text;
                ssa.Echos = new List<SSHEcho>();
                foreach(ListViewItem liv in this.lsvEchos.Items)
                {
                    SSHEcho se = new SSHEcho() { WriteStr = liv.Text, ScuessStr = liv.SubItems[1].Text, TimeOutSeconds = int.Parse(liv.SubItems[2].Text) };
                    ssa.Echos.Add(se);
                }
            };
        }

        public override string ActivityType => "litssh.ShellStreamActivity";
        ShellStreamActivity ssa = null;
        public override void SetActivity(Activity activity)
        {
            ssa = activity as ShellStreamActivity;
            this.tbHost.Text = ssa.Host;
            this.tbPort.Text = ssa.Port;
            this.tbUserName.Text = ssa.UserName;
            this.tbPassword.Text = ssa.Password;
            this.tbActivityName.Text = ssa.Name;

            foreach(SSHEcho se in ssa.Echos)
            {
                ListViewItem lvi = new ListViewItem() { Text = se.WriteStr };
                lvi.SubItems.Add(se.ScuessStr);
                lvi.SubItems.Add(se.TimeOutSeconds.ToString());
                this.lsvEchos.Items.Add(lvi);
            }

        }

        private void llbAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.pEchos.Visible = true;
            this.tbWriteStr.Clear();
            this.tbSucessStr.Clear();
            this.nudTimeOutSeconds.Value = 3;
            this.pEchos.Tag = null;
        }

        private void llbEdit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.lsvEchos.SelectedItems.Count == 1)
            {
                SSHEcho se = this.lsvEchos.SelectedItems[0].Tag as SSHEcho;
                this.pEchos.Visible = true;
                this.tbWriteStr.Text = se.WriteStr;
                this.tbSucessStr.Text = se.ScuessStr;
                this.nudTimeOutSeconds.Value = se.TimeOutSeconds;
                this.pEchos.Tag = se;
            }
        }

        private void llbDelete_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.lsvEchos.SelectedItems.Count == 0)
            {
                this.lsvEchos.SelectedItems[0].Remove();
            }
        }

        private void llbClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            pEchos.Visible = false;
        }

        private void lsvEchos_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.llbEdit.Enabled = this.llbDelete.Enabled = this.lsvEchos.SelectedItems.Count > 0;
            this.llbUp.Enabled = this.lsvEchos.SelectedItems.Count == 1 && this.lsvEchos.Items.Count > 1 && this.lsvEchos.SelectedIndices[0] > 0;
            this.llbDown.Enabled = this.lsvEchos.SelectedItems.Count == 1 && this.lsvEchos.Items.Count > 1 && this.lsvEchos.SelectedIndices[0] > this.lsvEchos.Items.Count - 1;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SSHEcho se = new SSHEcho() { WriteStr = this.tbWriteStr.Text, ScuessStr = this.tbSucessStr.Text, TimeOutSeconds = (int)this.nudTimeOutSeconds.Value };
            if (string.IsNullOrEmpty(se.WriteStr))
            {
                MessageBox.Show("输入命令不能为空");return;
            }
            ListViewItem lvi = new ListViewItem() { Text = se.WriteStr };
            lvi.SubItems.Add(se.ScuessStr);
            lvi.SubItems.Add(se.TimeOutSeconds.ToString());
            if (this.pEchos.Tag == null)
            {
                lsvEchos.Items.Add(lvi);
                this.pEchos.Visible = false;
            }
            else
            {
                if (this.lsvEchos.SelectedItems.Count == 1)
                {
                    int pos = this.lsvEchos.SelectedIndices[0];
                    this.lsvEchos.Items.RemoveAt(pos);
                    this.lsvEchos.Items.Insert(pos, lvi);
                    this.pEchos.Visible = false;
                }
                else
                {
                    MessageBox.Show("找不到编辑的记录，请检查");
                }
            }
        }
    }
}
